package cn.iocoder.yudao.module.erp.service.purchaseplan.purchaseequipmentdetail;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseequipmentdetail.PurchaseEquipmentDetailPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseequipmentdetail.PurchaseEquipmentDetailSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseplan.purchaseequipmentdetail.PurchaseEquipmentDetailDO;
import cn.iocoder.yudao.module.erp.dal.mysql.purchaseplan.purchaseequipmentdetail.PurchaseEquipmentDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PURCHASE_EQUIPMENT_DETAIL_NOT_EXISTS;

/**
 * 设备采购计划明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PurchaseEquipmentDetailServiceImpl implements PurchaseEquipmentDetailService {

    @Resource
    private PurchaseEquipmentDetailMapper purchaseEquipmentDetailMapper;

    @Override
    public void createPurchaseEquipmentDetailBatch(List<PurchaseEquipmentDetailSaveReqVO> createReqVOS) {
        // 插入
        List<PurchaseEquipmentDetailDO> purchaseEquipmentDetailS = BeanUtils.toBean(createReqVOS, PurchaseEquipmentDetailDO.class);
        purchaseEquipmentDetailMapper.insertBatch(purchaseEquipmentDetailS);
    }

    @Override
    public void updatePurchaseEquipmentDetail(PurchaseEquipmentDetailSaveReqVO updateReqVO) {
        // 校验存在
        validatePurchaseEquipmentDetailExists(updateReqVO.getId());
        // 更新
        PurchaseEquipmentDetailDO updateObj = BeanUtils.toBean(updateReqVO, PurchaseEquipmentDetailDO.class);
        purchaseEquipmentDetailMapper.updateById(updateObj);
    }

    @Override
    public void deletePurchaseEquipmentDetail(Long id) {
        // 校验存在
        validatePurchaseEquipmentDetailExists(id);
        // 删除
        purchaseEquipmentDetailMapper.deleteById(id);
    }

    @Override
        public void deletePurchaseEquipmentDetailListByIds(List<Long> ids) {
        // 校验存在
        validatePurchaseEquipmentDetailExists(ids);
        // 删除
        purchaseEquipmentDetailMapper.deleteByIds(ids);
        }

    private void validatePurchaseEquipmentDetailExists(List<Long> ids) {
        List<PurchaseEquipmentDetailDO> list = purchaseEquipmentDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PURCHASE_EQUIPMENT_DETAIL_NOT_EXISTS);
        }
    }

    private void validatePurchaseEquipmentDetailExists(Long id) {
        if (purchaseEquipmentDetailMapper.selectById(id) == null) {
            throw exception(PURCHASE_EQUIPMENT_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public PurchaseEquipmentDetailDO getPurchaseEquipmentDetail(Long id) {
        return purchaseEquipmentDetailMapper.selectById(id);
    }

    @Override
    public PageResult<PurchaseEquipmentDetailDO> getPurchaseEquipmentDetailPage(PurchaseEquipmentDetailPageReqVO pageReqVO) {
        return purchaseEquipmentDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByPlanId(Long planId) {
        purchaseEquipmentDetailMapper.deleteByPlanId(planId);
    }

    @Override
    public List<PurchaseEquipmentDetailDO> getByPlanId(Long planId) {
        return purchaseEquipmentDetailMapper.selectByPlanId(planId);
    }

}